package com.exercise;

import com.example.bean.ExamStudent;
import com.example.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Scanner;

public class Exercise2 {

    // 插入学生成绩
    @Test
    public void testInsert() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("四级/六级:");
        int type = scanner.nextInt();
        System.out.print("身份证号:");
        String idCard = scanner.next();
        System.out.print("考试证号:");
        String examCard = scanner.next();
        System.out.print("名称:");
        String name = scanner.next();
        System.out.print("城市:");
        String city = scanner.next();
        System.out.print("分数:");
        int grade = scanner.nextInt();

        String sql = "INSERT INTO `examstudent`(`Type`, `IDCard`, `ExamCard`, `StudentName`, `Location`, `Grade`) VALUES (?, ?, ?, ?, ?, ?)";
        boolean result = update(sql, type, idCard, examCard, name, city, grade);
        if (result) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }
    }

    // 通用的增删改操作
    public static boolean update(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 获取连接
            connection = JDBCUtils.getConnection();
            // 编译SQL
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            // 执行操作
            // preparedStatement.execute();
            return preparedStatement.executeUpdate() > 0;
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            // 释放资源
            JDBCUtils.closeResource(connection, preparedStatement);
        }
        return false;
    }

    // 查询学生成绩
    @Test
    public void testQuery() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类型:");
        System.out.println("a:身份证号");
        System.out.println("b:准考证号");
        String choice = scanner.next();

        if ("a".equalsIgnoreCase(choice)) {
            System.out.print("身份证号:");
            String idCard = scanner.next();
            String sql = "SELECT FlowID AS flowId, Type AS type, IDCard AS idCard, ExamCard AS examCard, " +
                    "StudentName AS studentName, Location AS location, Grade AS grade FROM examstudent WHERE IDCard = ?";
            ExamStudent examStudent = getInstance(ExamStudent.class, sql, idCard);
            if (examStudent == null) {
                System.out.println("查无此人，重新输入");
            } else {
                System.out.println(examStudent);
            }
        } else if ("b".equalsIgnoreCase(choice)) {
            System.out.print("准考证号:");
            String examCard = scanner.next();
            String sql = "SELECT FlowID AS flowId, Type AS type, IDCard AS idCard, ExamCard AS examCard, " +
                    "StudentName AS studentName, Location AS location, Grade AS grade FROM examstudent WHERE ExamCard = ?";
            ExamStudent examStudent = getInstance(ExamStudent.class, sql, examCard);
            if (examStudent == null) {
                System.out.println("查无此人，重新输入");
            } else {
                System.out.println(examStudent);
            }
        } else {
            System.out.println("输入有误");
        }
    }

    // 通用查询单条记录
    public static <T> T getInstance(Class<T> clazz, String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 获取连接
            connection = JDBCUtils.getConnection();

            // 编译SQL
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }

            // 查询结果
            resultSet = preparedStatement.executeQuery();
            // 获取结果集元数据
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();

            // 遍历结果集
            if (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = resultSet.getObject(i + 1);
                    // 获取列名
                    String columnName = resultSetMetaData.getColumnLabel(i + 1);

                    // 通过反射设置属性值
                    Field field = clazz.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                return t;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            // 释放资源
            JDBCUtils.closeResource(connection, preparedStatement, resultSet);
        }
        return null;
    }

    // 删除学生成绩
    @Test
    public void testDelete() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("考试号:");
        String examCard = scanner.next();

        String sql = "DELETE FROM examstudent WHERE ExamCard = ?";
        boolean result = update(sql, examCard);
        if (result) {
            System.out.println("删除成功");
        } else {
            System.out.println("查无此人，重新输入");
        }
    }

}
